/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SessionBeans.entites;

import SessionBeans.actions.OperationTypeEnum;
import SessionBeans.logs.FileChangeLogService;
import entities.File;
import entities.FileBlob;
import entities.Folder;
import java.math.BigDecimal;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tomas
 */
@Stateless
@LocalBean
public class FileService {

    @EJB
    private FileChangeLogService fileChangeLogService;
    @PersistenceContext(unitName = "tCloudEE-ejbPU")
    private EntityManager em;

    public File newFile(Long userId, String name, byte[] value, Long parentFolderId) {
        Folder parentFolder = em.find(Folder.class, parentFolderId);
        return newFile(userId, name, value, parentFolder);
    }

    public File newFile(Long userId, String name, byte[] vylue, Folder parentFolder) {
        File newFile = new File(null, name, 0);
        FileBlob fileBlob = new FileBlob();
        fileBlob.setValue(vylue);

        fileBlob = em.merge(fileBlob);
        newFile.setFileBlob(fileBlob.getId());
        newFile.setFolderId(parentFolder);

        newFile = em.merge(newFile);

        fileChangeLogService.addLogRow(userId, newFile, OperationTypeEnum.NEW);
        em.flush();
        return newFile;
    }

    public void updateFile(Long userId, Long fileId, byte[] value, String name) {
        File find = em.find(File.class, fileId);
        find.setFileName(name);
        updateFile(userId, find, value);
    }

    public void updateFile(Long userId, File file, byte[] value, String name) {
        file.setFileName(name);
        updateFile(userId, file, value);
    }

    public void updateFile(Long userId, Long fileId, byte[] value) {
        File find = em.find(File.class, fileId);

        updateFile(userId, find, value);
    }

    public void updateFile(Long userId, File file, byte[] value) {
        FileBlob find = em.find(FileBlob.class, file.getFileBlobId());

        find.setValue(value);
        em.merge(find);
        file.setVersion(file.getVersion() + 1);

        em.persist(file);

        fileChangeLogService.addLogRow(userId, file, OperationTypeEnum.UPDATE);
    }

    public void deleteFile(Long userId, Long fileId) {
        File find = em.find(File.class, fileId);

        deleteFile(userId, find);
    }

    public void deleteFile(Long userId, File file) {
        FileBlob findFileBlob = em.find(FileBlob.class, file.getFileBlobId());

        file.getFolderId().getFilesList().remove(file);
        file.setFolderId(null);

        em.remove(file);
        em.remove(findFileBlob);

        fileChangeLogService.addLogRow(userId, file, OperationTypeEnum.DETELE);
    }

    public Long loadFileSize(File file) {
        String dotaz = "select dbms_lob.getlength(value) as bytes from file_blob where id = '" + file.getFileBlobId() + "'";
        System.out.println(dotaz);
        Query createNativeQuery = em.createNativeQuery(dotaz);
        BigDecimal size = (BigDecimal) createNativeQuery.getSingleResult();

        return size.longValue();
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")    
}
